/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.d2g.realestate.facade;

import br.com.d2g.realestate.persistence.Agent;
import br.com.d2g.realestate.persistence.Client;
import br.com.d2g.realestate.persistence.Owner;
import br.com.d2g.realestate.persistence.User;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author d2g
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName = "WebApplication2PU")
    private EntityManager em;
    @EJB
    private AgentFacade agentFacade;
    @EJB
    private OwnerFacade ownerFacade;
    @EJB
    private ClientFacade clientFacade;

    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }

    public String cypher(String password) throws NoSuchAlgorithmException, Exception {
        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.reset();
        messageDigest.update(password.getBytes());
        byte[] mdBytes = messageDigest.digest();
        //convert the byte to hex format method 1
        StringBuilder stringBuilder = new StringBuilder();
        for (byte mdByte : mdBytes) {
            stringBuilder.append(Integer.toString((mdByte & 0xff) + 0x100, 16).substring(1));
        }
        String cryptedPass = stringBuilder.toString();
        return cryptedPass;
    }

    public boolean doLogin(User user) throws NoSuchAlgorithmException, Exception {
        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.reset();
        messageDigest.update(user.getPassword().getBytes(   ));
        byte[] mdBytes = messageDigest.digest();
        //convert the byte to hex format method 1
        StringBuilder stringBuilder = new StringBuilder();
        for (byte mdByte : mdBytes) {
            stringBuilder.append(Integer.toString((mdByte & 0xff) + 0x100, 16).substring(1));
        }
        String cryptedPass = stringBuilder.toString();
        User resultUser = em.find(User.class, user.getUsername());
        if (resultUser.getPassword().equals(cryptedPass)) {
            return true;
        }
        throw new Exception(ResourceBundle.getBundle("/Bundle").getString("LoginWrongUserPassword"));

    }

    public List<String> getRoles(User user) throws Exception {
//            Agent agent = new Agent();
//            agent.setUsername(user.getUsername());
        List<String> roles = new ArrayList<String>();
        Agent agent = agentFacade.find(user.getUsername());
        if (agent != null) {
            roles.add("agent");
        }
        Owner owner = ownerFacade.find(user.getUsername());
        if (owner != null) {
            roles.add("owner");
        }
        Client client = clientFacade.find(user.getUsername());
        if (client != null) {
            roles.add("client");
        }
        return roles;
    }
}
